package com.example.search.controller.v1;

import com.example.model.common.dtos.ResponseResult;
import com.example.model.search.dtos.UserSearchDto;
import com.example.search.service.ArticleSearchService;
import com.example.utils.common.AppJwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static org.reflections.Reflections.log;

@RestController
@RequestMapping("/api/v1/article/search")
@Slf4j
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    @PostMapping("/search")
    public ResponseResult search(@RequestBody UserSearchDto dto, HttpServletRequest request) throws IOException {

        String token = request.getHeader("ap_token");
        Jws<Claims> jws = AppJwtUtil.getJws(token);
        Claims claims = jws.getBody();
        String userId = claims.get("id").toString();
        log.info("登录用户id为: {}",userId);
        return articleSearchService.search(dto,userId);
//        return ResponseResult.okResult("1");
    }
}
